#include "callother.h"

CoValue *CoFunc_New(CoValue *name, int type, int num_arg)
{
    CoFunc *func;

    func = (CoFunc *)CO_MALLOC(sizeof(CoFunc));
    if (func == NULL) {
        /* TODO: [ERROR] no memory */
        return NULL;
    }
    COVALUE_INIT(func, &CoFunc_Type);

    func->f_name = name;
    CO_INCREF(name);

    func->f_type = type;
    func->f_num_arg = num_arg;
    func->f_num_local = 0;
    func->f_code_size = 0;
    func->f_code = 0;

    return (CoValue *)func;
}

void CoFunc_UpdateDeclaration(CoValue *self, int type, int num_arg)
{
    CoFunc *func;

    assert(COFUNC_CHECK(self));
    func = (CoFunc *)self;

    func->f_type = type;
    func->f_num_arg = num_arg;
}

void CoFunc_UpdateDefination(CoValue *self, int num_local, char *code_start, int code_size)
{
    CoFunc *func;

    assert(COFUNC_CHECK(self));
    func = (CoFunc *)self;

    func->f_num_local = num_local;
    func->f_code_size = code_size;
    func->f_code = (char *)CO_MALLOC(code_size * sizeof(char));
    memcpy(func->f_code, code_start, code_size);
}

static void func_dealloc(CoValue *self)
{
    CoFunc *func;

    assert(COFUNC_CHECK(self));
    func = (CoFunc *)self;

	CO_DECREF(func->f_name);

	CO_FREE(func->f_code);

	CoValue_Free(self);
}

CoType CoFunc_Type = {
    "function",
	func_dealloc,                   /* tp_dealloc */
    0,                              /* tp_hash */
    0,                              /* tp_equal */
    0,                              /* tp_str */
    0,                              /* tp_iter */
    0,                              /* tp_iternext */
};

